package com.example.aiquestions.controller;

import com.alibaba.fastjson.JSON;
import com.example.aiquestions.model.common.ApiResponse;
import com.example.aiquestions.service.QuestionLibraryService;
import com.example.aiquestions.service.QuestionService;
import org.springframework.web.bind.annotation.*;
import com.example.aiquestions.model.dto.QuestionDetail;
import com.example.aiquestions.model.dto.QuestionResponse.QuestionData;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.stream.Collectors;

@CrossOrigin
@RestController
@RequestMapping("/questions")
public class QuestionsController {
    
    @Autowired
    private QuestionService questionService;

    @Autowired
    private QuestionLibraryService libraryService;
    
    /**
     * 批量保存题目(也可单个保存)
     */
    @PostMapping("/save")
    public ApiResponse<List<Long>> addQuestions(@RequestParam("libraryId") String libraryId,@RequestBody List<QuestionDetail> questionDetails) {
        try {
            // 将QuestionDetail列表转换为QuestionData列表
            List<QuestionData> questionDataList = convertToQuestionDataList(questionDetails);
            // 调用service保存题目
            List<Long> result = questionService.saveQuestions(questionDataList);
            libraryService.addQuestionsToLibrary(libraryId, result);
            return ApiResponse.success(result);
        } catch (Exception e) {
            return ApiResponse.error(500,"保存题目失败：" + e.getMessage());
        }
    }


    /**
     *
     * @param id 题目id
     * @param questionDetail
     * @return
     */
    @PostMapping("/update")
    public ApiResponse<Boolean> updateQuestions(@RequestParam("id") Long id,
                                                @RequestBody QuestionDetail questionDetail) {
        QuestionData questionData = convertToQuestionData(questionDetail);
        try {
            questionService.updateQuestion(id,questionData);
            return ApiResponse.success(true);
        } catch (Exception e) {
            return ApiResponse.error(500,"更新题目失败：" + e.getMessage());
        }
    }
    @PostMapping("/delete")
    public ApiResponse<Boolean> deleteQuestions(@RequestParam("id") Long id) {
        try {
            questionService.deleteQuestionById(id);
            return ApiResponse.success(true);
        } catch (Exception e) {
            return ApiResponse.error(500,"删除题目失败：" + e.getMessage());
        }
    }
    
    /**
     * 将QuestionDetail列表转换为QuestionData列表
     */
    private List<QuestionData> convertToQuestionDataList(List<QuestionDetail> questionDetails) {
        return questionDetails.stream()
                .map(this::convertToQuestionData)
                .collect(Collectors.toList());
    }
    
    /**
     * 将单个QuestionDetail转换为QuestionData
     */
    private QuestionData convertToQuestionData(QuestionDetail detail) {
        QuestionData data = new QuestionData();
        
        // 设置基本字段
        data.setQuestionType(detail.getQuestionType());
        data.setStem(detail.getStem());
        data.setOptions(detail.getOptions());
        data.setAnswer(detail.getAnswer());
        data.setAnswers(detail.getAnswers());
        
        // 处理填空题答案
        if (detail.getBlanks() != null) {
            // 将字符串形式的填空答案转换为List
            List<String> blanksList = List.of(detail.getBlanks().split("\\|"));
            data.setBlanks(blanksList);
        }
        
        // 设置其他字段
        data.setPassage(detail.getPassage());
        data.setSubQuestions(detail.getSubQuestions());
        data.setAnalysis(detail.getAnalysis());
        data.setDifficulty(detail.getDifficulty());
        data.setTags(detail.getTags());
        data.setKnowledgePoints(detail.getKnowledgePoints());
        data.setKeyPoints(detail.getKeyPoints());
        data.setScore(detail.getScore());
        data.setTotalScore(detail.getTotalScore());
        
        return data;
    }
}
